package com.atguigu.eduservice.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.atguigu.eduservice.entity.EduSubject;
import com.atguigu.eduservice.entity.execl.ExcelSubjectData;
import com.atguigu.eduservice.service.EduSubjectService;
import com.atguigu.servicebase.handler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;

/**
 * @Auther 赵乾坤
 * @Date 2021-07-01 19:33
 */
@Api(description = "课程分类监听----》不能用组件管理，所以本类引入subjectService alt + insert")
public class SubjectExcelListener extends AnalysisEventListener<ExcelSubjectData> {
        //（1）监听器直接new，不能交给框架管理，service不能自动注入
        //（2）有参构造，手动注入service

    public static final String PARENTID = "0";


    private EduSubjectService subjectService;
    public SubjectExcelListener() { }

    //创建有参数构造，传递subjectService用于操作数据库
    public SubjectExcelListener(EduSubjectService subjectService) {
        this.subjectService = subjectService;
    }



    @Override
    //一行一行去读取excle内容
    public void invoke(ExcelSubjectData excelSubjectData, AnalysisContext analysisContext) {
        //1.读取数据验空
        if (excelSubjectData == null) {
            throw new GuliException(20001, "添加失败");
        }
        //2.判断一级分类名称是否重复（方法可以提出来）
        EduSubject existOneSubject = this.existOneSubject(excelSubjectData.getOneSubjectName(), subjectService);
        //3.不重复 插入数据
        if (existOneSubject == null) {
            existOneSubject = new EduSubject();
            //有父id的是1級,沒有父id设置id就是二级分类
            existOneSubject.setParentId(PARENTID);
            existOneSubject.setTitle(excelSubjectData.getOneSubjectName());
            subjectService.save(existOneSubject);
        }
        String pid = existOneSubject.getId();
        //4.判断二级分类名称是否重复（方法可以提出来）
        EduSubject existTwoSubject = this.existTwoSubject(excelSubjectData.getTwoSubjectName(), pid, subjectService);
        //5.不重复 插入数据
        if (existTwoSubject == null) {
            existTwoSubject =new EduSubject();
            existTwoSubject.setTitle(excelSubjectData.getTwoSubjectName());
            //有父id的是1級,沒有父id设置id就是二级分类
            existTwoSubject.setParentId(pid);
            subjectService.save(existTwoSubject);
        }






    }

    @Override
    //读取每一行之后的 写操作
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }
    //读取excel表头信息

    //判断一级分类是否重复
    private EduSubject existOneSubject(String oneSubjectName,EduSubjectService subjectService) {
        QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id", PARENTID);//parent_id = 0 就是一级的
        wrapper.eq("title",oneSubjectName);
        EduSubject oneSubject = subjectService.getOne(wrapper);
        return oneSubject;
    }


    //判断二级分类是否重复
    private EduSubject existTwoSubject(String twoSubjectName,String pid,EduSubjectService subjectService) {
        QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id", pid);//parent_id = 0 就是一级的
        wrapper.eq("title",twoSubjectName);
        EduSubject twoSubject = subjectService.getOne(wrapper);
        return twoSubject;


    }

}
